<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Licence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 
	//note: parent page needs to import db_connect for this file to work
	$weatherQuery = "SELECT zip_code, night_mode, night_start, night_end from tbl_weather";
	$weatherResult=mysql_query($weatherQuery);
	$weatherRow = mysql_fetch_array($weatherResult);
	
	$zipCode = 	$weatherRow['zip_code'];
	$nightMode = $weatherRow['night_mode'];
	$nightStart = $weatherRow['night_start'];
	$nightEnd = $weatherRow['night_end'];
	$isNight = false;

	$xml = @simplexml_load_file('http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query='.$zipCode);
	
	if($xml != false){
		$information = $xml->xpath("/current_observation");
		$icons = $xml->xpath("/current_observation/icons");
		
		//if night mode is 'Y' we need to check if it's night
		if($nightMode == 'Y'){
			$localEpochTime = $information[0]->local_epoch;
			$startNight = strtotime(date('Y/m/d').' '.$nightStart);
			$plusDay = "";
			
			if($nightStart>$nightEnd){
				$plusDay = " +1 day";
			}	
			$endNight =  strtotime(date('Y/m/d').' '.$nightEnd.$plusDay);
			
			$isNight = ($localEpochTime >= $startNight && $localEpochTime <= $endNight);
		}
?>
		<table><tr>
		<td>
		<?php
		$iconName = $information[0]->icon;
		
		if($isNight){
			$iconName.='-night';
		}
		
		echo '<img src="/images/weather/weather_icons/'.$iconName;
	
		
	
		echo '.png" alt="weather" height="40px" width="40px" style="vertical-align:middle;"/>';
		?>
		</td>
		<td>
		<?php
		echo '<span id="weatherIcon" iconName="'.$iconName.'" style="vertical-align:middle;" style="font-size:30px;color:#FFFFFF;">'.$information[0]->temp_f.'&deg; F</span>';
		?>
		</td>
		</tr>
		</table>
<?php		
	}else{
		echo '!ERROR!';
	}
?>